package com.cskaoyan.market.admin.service.impl;

import com.cskaoyan.market.admin.service.AdminOrderService;
import com.cskaoyan.market.db.domain.*;
import com.cskaoyan.market.db.mapper.MarketOrderGoodsMapper;
import com.cskaoyan.market.db.mapper.MarketOrderMapper;
import com.cskaoyan.market.db.mapper.MarketUserMapper;
import com.cskaoyan.market.db.vo.MarketChannelVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class AdminOrderServiceImpl implements AdminOrderService {
    @Autowired
    private MarketOrderMapper mapper;
    @Autowired
    private MarketOrderGoodsMapper mapper1;

    @Autowired
    private MarketUserMapper mapper2;
    /**
     * 订单查询
     * @return List<MarketOrder> 订单列表
     */
    @Override
    public List<MarketOrder> list(Integer page, Integer limit, String sort, String order, LocalDateTime start, LocalDateTime end, List<Short> orderStatusArray, Integer userId, String orderSn) {


        // 创建实例对象
        MarketOrderExample marketOrderExample = new MarketOrderExample();
        // 设置排序
        marketOrderExample.setOrderByClause(sort +" "+ order);
        // 设置查询条件
        MarketOrderExample.Criteria criteria = marketOrderExample.or();
        if (start != null) criteria.andAddTimeGreaterThan(start);
        if (end != null) criteria.andAddTimeLessThan(end);
        if (userId != null) criteria.andUserIdEqualTo(userId);
        if (orderSn != null) criteria.andOrderSnEqualTo(orderSn);
        if (orderStatusArray != null) criteria.andOrderStatusIn(orderStatusArray);
        criteria.andDeletedEqualTo(false).example();
        // 获取查询结果
        List<MarketOrder> marketOrders = mapper.selectByExample(marketOrderExample);
        // 收尾
        
        
        return marketOrders;
    }
    @Override
    public List<MarketChannelVo> channel() {

        MarketOrderExample marketOrderExample = new MarketOrderExample();
        marketOrderExample.or().andShipChannelEqualToColumn(MarketOrder.Column.shipChannel);
        List<MarketOrder> orderList = mapper.selectByExample(marketOrderExample);
        List<MarketChannelVo> marketChannelVoList = new ArrayList<>();

        for (MarketOrder marketOrder : orderList) {
            String channel = marketOrder.getShipChannel();
            MarketChannelVo channelVo = new MarketChannelVo();
            channelVo.setCode(channel);
            if ("ZTO".equals(channel)) {
                channelVo.setName("中通快递");
            }
            if ("YTO".equals(channel)) {
                channelVo.setName("圆通速递");
            }
            if ("YD".equals(channel)) {
                channelVo.setName("韵达速递");
            }
            if ("YZPY".equals(channel)) {
                channelVo.setName("邮政快递包裹");
            }
            if ("EMS".equals(channel)) {
                channelVo.setName("EMS");
            }
            if ("DBL".equals(channel)) {
                channelVo.setName("德邦快递");
            }
            if ("FAST".equals(channel)) {
                channelVo.setName("快捷快递");
            }
            if ("ZJS".equals(channel)) {
                channelVo.setName("宅急送");
            }
            if ("TNT".equals(channel)) {
                channelVo.setName("TNT快递");
            }
            if ("UPS".equals(channel)) {
                channelVo.setName("UPS");
            }
            if ("DHL".equals(channel)) {
                channelVo.setName("DHL");
            }
            if ("FEDEX".equals(channel)) {
                channelVo.setName("FEDEX联邦(国内件)");
            }
            if ("FEDEX_GJ".equals(channel)) {
                channelVo.setName("FEDEX联邦(国际件)");
            }

            marketChannelVoList.add(channelVo);
        }
        return marketChannelVoList;
    }

    /**
     * 订单细节
     * @param id 订单id
     * @return 细节数据
     */
    @Override
    public Map<String, Object> detail(Integer id) {

        // 查询对应id对应订单
        MarketOrder marketOrder = mapper.selectByPrimaryKey(id);

        // 创建实例对象
        MarketOrderGoodsExample marketOrderGoodsExample = new MarketOrderGoodsExample();
        // 设置查询条件
        MarketOrderGoodsExample.Criteria criteria1 = marketOrderGoodsExample.or();
        criteria1.andOrderIdEqualTo(id);
        criteria1.andDeletedEqualTo(false);
        List<MarketOrderGoods> marketOrderGoods = mapper1.selectByExample(marketOrderGoodsExample);

        // 创建实例对象
        MarketUser marketUser = mapper2.selectByPrimaryKey(marketOrder.getUserId());
        // 将marketUser改装成返回对象
        HashMap<String, String> user = new HashMap<>();
        user.put("nickname", marketUser.getNickname());
        user.put("avatar", marketUser.getAvatar());

        // 创建返回对象
        HashMap<String, Object> map = new HashMap<>();
        map.put("orderGoods", marketOrderGoods);
        map.put("user", user);
        map.put("order", marketOrder);
        return map;
    }

    /**
     * 发货修改订单
     * @param marketOrder 待修改订单对象
     * @return 操作状态
     */
    @Override
    public int ship(MarketOrder marketOrder) {
        // 获取映射文件
        
        
        // 修改数据
        int i = mapper.updateByPrimaryKeySelective(marketOrder);
        return i;
    }

    /**
     * 退款修改订单
     * @param marketOrder 待修改订单对象
     * @return 操作状态
     */
    @Override
    public int refund(MarketOrder marketOrder) {
        // 获取映射文件
        
        
        // 修改数据
        int i = mapper.updateByPrimaryKeySelective(marketOrder);
        return i;
    }

    /**
     * 删除订单
     * @param orderId 待删除订单id
     * @return 操作状态
     */
    @Override
    public int delete(Integer orderId) {
        // 获取映射文件
        
        
        // 创建待修改对象
        MarketOrder marketOrder = new MarketOrder();
        marketOrder.setId(orderId);
        marketOrder.setDeleted(true);
        // 修改数据
        int i = mapper.updateByPrimaryKeySelective(marketOrder);
        // 收尾
        
        
        return i;
    }
}
